﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace b4.形状的合法性和面积
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Rectangle a = new Rectangle(2.4, 2.4);
            if (a.Islegal())
                Console.WriteLine("长方形面积为：" + a.Cal_area());
            else
                Console.WriteLine("长方形形状不合法！");
            Console.WriteLine();

            Square b = new Square(5);
            if (b.Islegal())
                Console.WriteLine("正方形面积为：" + b.Cal_area());
            else
                Console.WriteLine("正方形形状不合法！");
            Console.WriteLine();

            Triangle c = new Triangle(3, 4, 5);
            if (c.Islegal())
                Console.WriteLine("三角形面积为：" + c.Cal_area());
            else
                Console.WriteLine("三角形形状不合法！");
            Console.WriteLine();
            Console.ReadLine();
        }
    }
}

interface shape     //定义接口，有两个方法
{
    bool Islegal();     //判断是否合法
    double Cal_area();     //计算面积
}

class Triangle : shape
{
    protected double L1;
    protected double L2;
    protected double L3;

    public Triangle(double L1, double L2, double L3)
    {
        this.L1 = L1;
        this.L2 = L2;
        this.L3 = L3;
    }

    public bool Islegal()
    {
        if (this.L1 <= 0 || this.L2 <= 0 || this.L3 <= 0)
            return false;
        else if (this.L1 + this.L2 <= this.L3)
            return false;
        else if (this.L1 + this.L3 <= this.L2)
            return false;
        else if (this.L2 + this.L3 <= this.L1)
            return false;
        else
            return true;
    }

    public double Cal_area()
    {
        //利用海伦公式求面积
        if (!this.Islegal())
        {
            Console.WriteLine("形状不合法，无法计算面积！");
            return -1;
        }
        else
        {
            double p, S;
            p = (L1 + L2 + L3) / 2;
            S = p * (p - L1) * (p - L2) * (p - L3);
            S = Math.Sqrt(S);
            return S;
        }
    }
}




class Rectangle : shape     //长方形类继承接口类shape
{
    protected double Length;
    protected double Width;

    public Rectangle(double Length, double Width)
    {
        this.Length = Length;
        this.Width = Width;
    }

    public bool Islegal()
    {
        if (this.Length <= 0 || this.Width <= 0)
            return false;
        else
            return true;
    }

    public double Cal_area()
    {
        double area;
        if (!this.Islegal())
        {
            Console.WriteLine("形状不合法，无法计算面积！");
            return -1;
        }
        else
        {
            area = this.Length * this.Width;
            return area;
        }
    }
}

class Square : Rectangle    //正方形是一种特殊的长方形，所以直接继承Rectangle类
{
    public Square(double edge):base(edge, edge) { }    //调用父类的构造方法，使长宽相等

}